// xc_lshop/business/order/com/refund.js
var actbill = require('../../model/business_bill')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    op: "",
    modellistindex: -1,
    balancepay: 0,
    onlinepay: 0,
    popupblock: false,
    refundata: {
      balance: 0,
      online: 0,
      id: 0,
      ordernum: ""
    },
    refusedata: {
      id: "",
      reason: "",
      ordernum: ""
    },
    errormessage: "",
    pricedata: {
      id: "",
      totalprice: 0,
      discountprice: 0,
      ordernum: ""
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showrefund(op, index, item) {

      var updatedata = {
        op: op,
        popupblock: true,
        modellistindex: index,
        errormessage: ""
      }
    
      if (op == 'refund') {
        updatedata['balancepay'] = item.balancepay
        updatedata['onlinepay'] = item.onlinepay
        updatedata['refundata.id'] = item.id
        updatedata['refundata.ordernum'] = item.ordernum

        if (item.balancepay > 0) {
          updatedata['refundata.balance'] = (item.balancepay / 100).toFixed(2)
        }else{
          updatedata['refundata.balance'] =0
        }
        if (item.onlinepay > 0) {
          updatedata['refundata.online'] = (item.onlinepay / 100).toFixed(2)
        }else{
          updatedata['refundata.online'] = 0
        }
        console.log()
      } else if (op == "refuse") {
        updatedata['refusedata.id'] = item.id
        updatedata['refusedata.ordernum'] = item.ordernum
      } else if (op == "changeprice") {
        updatedata['pricedata.id'] = item.id
        updatedata['pricedata.ordernum'] = item.ordernum
        updatedata['pricedata.discountprice'] = (item.discountprice / 100).toFixed(2)

        updatedata['pricedata.totalprice'] = (item.totalprice / 100).toFixed(2)


      }



      this.setData(updatedata)
    },
    bindKeyInput(e) {
      var dataset = e.target.dataset;
      var name = dataset.name;
      var olddata =0;
      if(name=='pricedata.discountprice'){
         olddata = this.data.pricedata.discountprice
      }else if(name=='refundata.online'){
        olddata = this.data.refundata.online
      }else if(name=='refundata.balance'){
        olddata = this.data.refundata.balance
      }
      var val = e.detail.value

      var updatestatus = 1;
      if (val * 1 == olddata) {
        updatestatus = -1;
      }

      if (updatestatus == 1) {
        val = val * 1;
        var min = dataset.min * 1;
        var max = dataset.max * 1;
        if (val < min) {
          val = min;
        } else if (val > max) {
          val = max;
        }
        var dot = String(val).indexOf(".");
        if (dot != -1) {
          var dotCnt = String(val).substring(dot + 1, val.length);
          if (dotCnt.length > 2) {
            val = val.toFixed(2);
          }
        }
        this.setData({
          [name]: val
        })
      }
    },
    bindTextInput(e) {
      
      var updata = {};
      updata[e.currentTarget.dataset.name] = e.detail.value;
      this.setData(updata)
    },
    onClose() {
      this.setData({
        popupblock: false
      })
    },
    formSubmit(e) {
      var that = this;
      actbill.orderrefund(this.data.refundata).then(res => {
    
        if (res["status"] == 1) {

          wx.showToast({
            icon: 'success',
            title: "退款成功",
          })
          that.setData({
            popupblock: false
          })
          // var val = e.detail.value; //通过这个传递数据
          var myEventDetail = {
            op: that.data.op,
            index: that.data.modellistindex,
            refundstatus: 1, //已退款
            refundata: that.data.refundata
          }
          this.triggerEvent('eventpopup', myEventDetail) //myevent自定义名称事件，父组件中使用
        } else {
          wx.showToast({
            icon: 'error',
            title: res["message"],
          })
        }
      })
    },
    formRefuseSubmit(e) {
      var that = this;
      console.log(that.data.refusedata)
      if (that.data.refusedata.reason) {
        actbill.orderrefuse(this.data.refusedata).then(res => {
        
          if (res["status"] == 1) {

            wx.showToast({
              icon: 'success',
              title: "已拒绝",
            })
            that.setData({
              popupblock: false
            })
            // var val = e.detail.value; //通过这个传递数据
            var myEventDetail = {
              op: that.data.op,
              index: that.data.modellistindex,
              refundstatus: 3, //已拒绝
              refusedata: that.data.refusedata
            }
            this.triggerEvent('eventpopup', myEventDetail) //myevent自定义名称事件，父组件中使用
          } else {
            wx.showToast({
              icon: 'error',
              title: res["message"],
            })
          }
        })
      } else {
        this.setData({
          errormessage: "拒绝原因不为空"
        })
      }
    },
    formPriceSubmit(e) {
      var that = this;
      
      actbill.orderchangeprice(this.data.pricedata).then(res => {
     
        if (res["status"] == 1) {
          wx.showToast({
            icon: 'success',
            title: "改价成功",
          })
          that.setData({
            popupblock: false
          })
          // var val = e.detail.value; //通过这个传递数据
          var myEventDetail = {
            op: that.data.op,
            index: that.data.modellistindex,
            pricedata: that.data.pricedata
          }
          this.triggerEvent('eventpopup', myEventDetail) //myevent自定义名称事件，父组件中使用
        } else {
          wx.showToast({
            icon: 'error',
            title: res["message"],
          })
        }
      })
    }
  }
})